package com.photoeditor.demo.model.bean.face;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lyzirving
 * time        2018/12/20
 * email       lyzirving@sina.com
 * information
 */
public class CommonFaceBean extends BaseFaceScanBean implements Parcelable {

    public List<FacePoint> faceOval;
    public List<FacePoint> leftEyeBrowTop;
    public List<FacePoint> leftEyeBrowBottom;
    public List<FacePoint> rightEyeBrowTop;
    public List<FacePoint> rightEyeBrowBottom;
    public List<FacePoint> leftEye;
    public List<FacePoint> rightEye;
    public List<FacePoint> upperLipTop;
    public List<FacePoint> upperLipBottom;
    public List<FacePoint> lowLipTop;
    public List<FacePoint> lowLipBottom;
    public List<FacePoint> noseBridge;
    public List<FacePoint> noseBottom;


    public CommonFaceBean newBean() {
        CommonFaceBean newBean = new CommonFaceBean();
        newBean.faceOval = newContour(faceOval);
        newBean.leftEyeBrowTop = newContour(leftEyeBrowTop);
        newBean.leftEyeBrowBottom = newContour(leftEyeBrowBottom);
        newBean.rightEyeBrowTop = newContour(rightEyeBrowTop);
        newBean.rightEyeBrowBottom = newContour(rightEyeBrowBottom);
        newBean.leftEye = newContour(leftEye);
        newBean.rightEye = newContour(rightEye);
        newBean.upperLipTop = newContour(upperLipTop);
        newBean.upperLipBottom = newContour(upperLipBottom);
        newBean.lowLipTop = newContour(lowLipTop);
        newBean.lowLipBottom = newContour(lowLipBottom);
        newBean.noseBridge = newContour(noseBridge);
        newBean.noseBottom = newContour(noseBottom);
        return newBean;
    }

    private List<FacePoint> newContour(List<FacePoint> src) {
        List<FacePoint> dst = new ArrayList<>();
        for (FacePoint facePoint : src) {
            dst.add(new FacePoint(facePoint));
        }
        return dst;
    }

    public void scaleScreenCoord(float xRatio, float yRatio) {
        for (int i = 0; i < faceOval.size(); i++) {
            FacePoint facePoint = faceOval.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < leftEyeBrowTop.size(); i++) {
            FacePoint facePoint = leftEyeBrowTop.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < leftEyeBrowBottom.size(); i++) {
            FacePoint facePoint = leftEyeBrowBottom.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < rightEyeBrowTop.size(); i++) {
            FacePoint facePoint = rightEyeBrowTop.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < rightEyeBrowBottom.size(); i++) {
            FacePoint facePoint = rightEyeBrowBottom.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < leftEye.size(); i++) {
            FacePoint facePoint = leftEye.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < rightEye.size(); i++) {
            FacePoint facePoint = rightEye.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < upperLipTop.size(); i++) {
            FacePoint facePoint = upperLipTop.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < upperLipBottom.size(); i++) {
            FacePoint facePoint = upperLipBottom.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < lowLipTop.size(); i++) {
            FacePoint facePoint = lowLipTop.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < lowLipBottom.size(); i++) {
            FacePoint facePoint = lowLipBottom.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < noseBridge.size(); i++) {
            FacePoint facePoint = noseBridge.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
        for (int i = 0; i < noseBottom.size(); i++) {
            FacePoint facePoint = noseBottom.get(i);
            facePoint.x = facePoint.x * xRatio;
            facePoint.y = facePoint.y * yRatio;
        }
    }

    public void translateScreenCoord(float dx, float dy) {
        for (int i = 0; i < faceOval.size(); i++) {
            FacePoint facePoint = faceOval.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < leftEyeBrowTop.size(); i++) {
            FacePoint facePoint = leftEyeBrowTop.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < leftEyeBrowBottom.size(); i++) {
            FacePoint facePoint = leftEyeBrowBottom.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < rightEyeBrowTop.size(); i++) {
            FacePoint facePoint = rightEyeBrowTop.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < rightEyeBrowBottom.size(); i++) {
            FacePoint facePoint = rightEyeBrowBottom.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < leftEye.size(); i++) {
            FacePoint facePoint = leftEye.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < rightEye.size(); i++) {
            FacePoint facePoint = rightEye.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < upperLipTop.size(); i++) {
            FacePoint facePoint = upperLipTop.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < upperLipBottom.size(); i++) {
            FacePoint facePoint = upperLipBottom.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < lowLipTop.size(); i++) {
            FacePoint facePoint = lowLipTop.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < lowLipBottom.size(); i++) {
            FacePoint facePoint = lowLipBottom.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < noseBridge.size(); i++) {
            FacePoint facePoint = noseBridge.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
        for (int i = 0; i < noseBottom.size(); i++) {
            FacePoint facePoint = noseBottom.get(i);
            facePoint.x = facePoint.x + dx;
            facePoint.y = facePoint.y + dy;
        }
    }

    public void transScreenCoord2GlCoord() {
        for (int i = 0; i < faceOval.size(); i++) {
            FacePoint facePoint = faceOval.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < leftEyeBrowTop.size(); i++) {
            FacePoint facePoint = leftEyeBrowTop.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < leftEyeBrowBottom.size(); i++) {
            FacePoint facePoint = leftEyeBrowBottom.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < rightEyeBrowTop.size(); i++) {
            FacePoint facePoint = rightEyeBrowTop.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < rightEyeBrowBottom.size(); i++) {
            FacePoint facePoint = rightEyeBrowBottom.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < leftEye.size(); i++) {
            FacePoint facePoint = leftEye.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < rightEye.size(); i++) {
            FacePoint facePoint = rightEye.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < upperLipTop.size(); i++) {
            FacePoint facePoint = upperLipTop.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < upperLipBottom.size(); i++) {
            FacePoint facePoint = upperLipBottom.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < lowLipTop.size(); i++) {
            FacePoint facePoint = lowLipTop.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < lowLipBottom.size(); i++) {
            FacePoint facePoint = lowLipBottom.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < noseBridge.size(); i++) {
            FacePoint facePoint = noseBridge.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
        for (int i = 0; i < noseBottom.size(); i++) {
            FacePoint facePoint = noseBottom.get(i);
            facePoint.x = (facePoint.x * 2f - 1f);
            facePoint.y = ((1-facePoint.y) * 2f - 1f);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeTypedList(this.faceOval);
        dest.writeTypedList(this.leftEyeBrowTop);
        dest.writeTypedList(this.leftEyeBrowBottom);
        dest.writeTypedList(this.rightEyeBrowTop);
        dest.writeTypedList(this.rightEyeBrowBottom);
        dest.writeTypedList(this.leftEye);
        dest.writeTypedList(this.rightEye);
        dest.writeTypedList(this.upperLipTop);
        dest.writeTypedList(this.upperLipBottom);
        dest.writeTypedList(this.lowLipTop);
        dest.writeTypedList(this.lowLipBottom);
        dest.writeTypedList(this.noseBridge);
        dest.writeTypedList(this.noseBottom);
    }

    public CommonFaceBean() {
    }

    protected CommonFaceBean(Parcel in) {
        super(in);
        this.faceOval = in.createTypedArrayList(FacePoint.CREATOR);
        this.leftEyeBrowTop = in.createTypedArrayList(FacePoint.CREATOR);
        this.leftEyeBrowBottom = in.createTypedArrayList(FacePoint.CREATOR);
        this.rightEyeBrowTop = in.createTypedArrayList(FacePoint.CREATOR);
        this.rightEyeBrowBottom = in.createTypedArrayList(FacePoint.CREATOR);
        this.leftEye = in.createTypedArrayList(FacePoint.CREATOR);
        this.rightEye = in.createTypedArrayList(FacePoint.CREATOR);
        this.upperLipTop = in.createTypedArrayList(FacePoint.CREATOR);
        this.upperLipBottom = in.createTypedArrayList(FacePoint.CREATOR);
        this.lowLipTop = in.createTypedArrayList(FacePoint.CREATOR);
        this.lowLipBottom = in.createTypedArrayList(FacePoint.CREATOR);
        this.noseBridge = in.createTypedArrayList(FacePoint.CREATOR);
        this.noseBottom = in.createTypedArrayList(FacePoint.CREATOR);
    }

    public static final Creator<CommonFaceBean> CREATOR = new Creator<CommonFaceBean>() {
        @Override
        public CommonFaceBean createFromParcel(Parcel source) {
            return new CommonFaceBean(source);
        }

        @Override
        public CommonFaceBean[] newArray(int size) {
            return new CommonFaceBean[size];
        }
    };
}
